const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
const baseUrl = 'http://192.168.100.128:9999'
// const baseUrl = 'http://test1.mgr.s-gsun.com'
const resolve = (dir) => {
  return path.join(__dirname, dir)
}

module.exports = {
  // assetsDir: '/',
  // outputDir: 'dist',
  // assetsDir: 'static',
  css: {
    loaderOptions: {
      scss: {
        data: '@import "@/assets/styles/variables.scss";'
      }
    }
  },
  devServer: {
    port: 5555,
    proxy: {
      '/auth': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/auth': '/auth'
        }
      },
      '/fs': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/fs': '/fs'
        }
      },
      '/admin': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/admin': '/admin'
        }
      },
      '/crm': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/crm': '/crm'
        }
      },
      '/distrition': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/distrition': '/distrition'
        }
      },
      '/projectdb': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/projectdb': '/projectdb'
        }
      },
      '/project': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/project': '/project'
        }
      },
      '/cd': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/cd': '/cd'
        }
      },
      '/ub': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/ub': '/ub'
        }
      },
      '/mgr': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/mgr': '/mgr'
        }
      },
      '/contract': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/contract': '/contract'
        }
      },
      '/hscode': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/hscode': '/hscode'
        }
      },
      '/cmd': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/cmd': '/cmd'
        }
      },
      '/geye': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/geye': '/geye'
        }
      },
      '/gmine': {
        target: baseUrl,
        changeOrigin: true,
        ws: false,
        secure: false,
        pathRewrite: {
          '^/gmine': '/gmine'
        }
      }
    }
  },
  configureWebpack(config) {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    config.name = '聚合'
    config.externals = {
      vue: 'Vue',
      'element-ui': 'ELEMENT',
      'samples-ui': 'SAMPLES'
    }
  },
  chainWebpack(config) {
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...
      config.optimization.minimizer([
        new TerserPlugin({
          terserOptions: {
            warnings: true,
            parallel: true,
            compress: {
              drop_console: !(process.env.CDN_HOST.startsWith('dev') || process.env.CDN_HOST.startsWith('test')), // 判断，当在dev,和,test环境时，不移除console,仅在pre,正式环境下移除
              drop_debugger: true // 移除debugger
            }
          }
        })
      ])
    } else {
      // 为开发环境修改配置...
    }
    const cdn = {
      css: [
        // element-ui css
        process.env.NODE_ENV === 'development' ? 'http://dev.static.s-gsun.com/libs/element/2.8.2/index.css' : `//${process.env.CDN_HOST}/libs/element/2.8.2/index.css`,
        // samples-ui css
        process.env.NODE_ENV === 'development' ? `http://dev.static.s-gsun.com/css/samples-ui@1.0.3.css` : `//${process.env.CDN_HOST}/css/samples-ui@1.0.3.css`
      ],
      js: [
        // vue must at first!
        process.env.NODE_ENV === 'development' ? 'http://dev.static.s-gsun.com/libs/vue/2.6.10/vue.js' : `//${process.env.CDN_HOST}/libs/vue/2.6.10/vue.min.js`,
        // element-ui js
        process.env.NODE_ENV === 'development' ? 'http://dev.static.s-gsun.com/libs/element/2.8.2/index.js' : `//${process.env.CDN_HOST}/libs/element/2.8.2/index.js`,
        // samples-ui js
        process.env.NODE_ENV === 'development' ? `http://dev.static.s-gsun.com/js/samples-ui@1.0.3.js` : `//${process.env.CDN_HOST}/js/samples-ui@1.0.3.js`
      ]
    }
    /* config.module
      .rule('images')
      .test(/\.(jpg|png|gif)$/)
      .use('url-loader')
      .loader('url-loader')
      .options({
        limit: 10,
        publicPath,
        name: '[name].[hash:8].[ext]'
      })
      .end() */
    config.plugin('html')
      .tap(args => {
        args[0].cdn = cdn
        args[0].minify = false
        return args
      })
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    // 增加运行时process.env的环境变量
    config.plugin('define')
      .tap(args => {
        args[0]['process.env'].CDN_HOST = `"${process.env.CDN_HOST}"`
        return args
      }).end()
    /* config.externals({
      vue: 'Vue'
    }) */
  }
}
